/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：RegisterDialog.java <br>
 * 创建时间：2013-5-21 下午4:07:07 <br>
 * 所属项目：GmudEX <br>
 * @author 12548 <br>
 */
package lostland.gmud.exv2.ui.meta;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import cn.bmob.v3.listener.SaveListener;
import lostland.gmud.exv2.Game;
import lostland.gmud.exv2.R;
import lostland.gmud.exv2.community.GmudUser;

/**
 * 类名：RegisterDialog <br>
 * 说明：注册对话框
 *
 * @author 12548
 */
public class LoginDialog extends Dialog {

	EditText etUsername;

	private EditText etPassword;


	/**
	 * @param context
	 */
	public LoginDialog(Context context) {
		super(context);
		Window mWindow = getWindow();
		mWindow.setBackgroundDrawable(new ColorDrawable(0));
		mWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
		WindowManager.LayoutParams lp = mWindow.getAttributes();
		lp.gravity = Gravity.CENTER;
		lp.dimAmount = 0.0001f;
		mWindow.setAttributes(lp);
		setCanceledOnTouchOutside(true);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		mWindow.setContentView(R.layout.dialog_login);


		TextView tvTitle = (TextView) findViewById(R.id.textView1);
		tvTitle.setText("登录");
		etUsername = (EditText) findViewById(R.id.etUsername);
		etPassword = (EditText) findViewById(R.id.etPassword);

		Button btn = (Button) findViewById(R.id.btnRegister);
		btn.setText("登录");
		btn.setOnClickListener(new View.OnClickListener() {

			                       @Override
			                       public void onClick(View v) {
				                       GmudUser bu = new GmudUser();
				                       bu.setUsername(etUsername.getText().toString());
				                       bu.setPassword(etPassword.getText().toString());
				                       Toast.makeText(Game.getInstance(), "登录中......", Toast.LENGTH_SHORT).show();
				                       bu.login(Game.getInstance(), new SaveListener() {
					                       @Override
					                       public void onSuccess() {
						                       Toast.makeText(Game.getInstance(), "登录成功！", Toast.LENGTH_SHORT).show();
//						GmudGame.redirectToCommunityScreen();
						                       Game.getInstance().popToRootScreen();
						                       dismiss();
					                       }

					                       @Override
					                       public void onFailure(int code, String msg) {
						                       Toast.makeText(Game.getInstance(), "登录失败！" + msg, Toast.LENGTH_SHORT).show();
					                       }
				                       });
			                       }
		                       }
		);
	}


	/* （非 Javadoc）
	 * @see android.app.Dialog#cancel()
	 */
	@Override
	public void cancel() {
		super.cancel();
		Game.getInstance().popToRootScreen();
	}

}
